<template>
  <div class="login-container">
    <!-- 背景装饰 -->
    <div class="bg-decoration">
      <div class="floating-shape shape-1"></div>
      <div class="floating-shape shape-2"></div>
      <div class="floating-shape shape-3"></div>
    </div>
    
    <el-card class="login-card" :class="{ 'card-entrance': cardVisible }">
      <template #header>
        <div class="card-header">
          <el-icon class="header-icon"><Shop /></el-icon>
          <span class="title">餐厅管理系统</span>
          <div class="subtitle">欢迎回来，请登录您的账户</div>
        </div>
      </template>
      
      <el-form ref="formRef" :model="loginForm" :rules="rules" label-width="0" class="login-form">
        <el-form-item prop="username">
          <el-input 
            v-model="loginForm.username" 
            placeholder="请输入用户名" 
            clearable
            size="large"
            class="custom-input"
          >
            <template #prefix>
              <el-icon class="input-icon"><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            show-password
            clearable
            size="large"
            class="custom-input"
            @keyup.enter="handleLogin"
          >
            <template #prefix>
              <el-icon class="input-icon"><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        
        <el-form-item>
          <el-button 
            type="primary" 
            :loading="loading" 
            @click="handleLogin" 
            class="login-btn"
            size="large"
          >
            <span v-if="!loading">登录</span>
            <span v-else>登录中...</span>
          </el-button>
        </el-form-item>
        
        <!-- 其他登录方式 -->
        <el-form-item>
          <div class="alternative-login">
            <div class="custom-divider">
              <div class="divider-line"></div>
              <span class="divider-text">其他方式登录</span>
              <div class="divider-line"></div>
            </div>
            <div class="social-login">
              <el-button circle class="social-btn wechat-btn" @click="handleWechatLogin">
                <el-icon><Message /></el-icon>
              </el-button>
              <el-button circle class="social-btn qq-btn" @click="handleQQLogin">
                <el-icon><Avatar /></el-icon>
              </el-button>
              <el-button circle class="social-btn phone-btn" @click="handlePhoneLogin">
                <el-icon><Phone /></el-icon>
              </el-button>
            </div>
          </div>
        </el-form-item>
        
        <el-form-item>
          <div class="footer-links">
            <span>还没有账号？</span>
            <el-link type="primary" @click="goToRegister" class="register-link">
              立即注册
            </el-link>
          </div>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { useUserStore } from '@/stores/user';
import { Shop, User, Lock, Message, Avatar, Phone } from '@element-plus/icons-vue';
import type { FormInstance, FormRules } from 'element-plus';
import { ElMessage } from 'element-plus';

const router = useRouter();
const userStore = useUserStore();
const formRef = ref<FormInstance>();
const loading = ref(false);
const cardVisible = ref(false);

const loginForm = reactive({
  username: '',
  password: ''
});

const rules: FormRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 30, message: '用户名长度在 3 到 30 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度至少为 6 个字符', trigger: 'blur' }
  ]
};

const handleLogin = async () => {
  if (!formRef.value) return;
  
  await formRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true;
      try {
        const success = await userStore.login(loginForm);
        if (success) {
          // 根据用户类型跳转不同页面
          if (userStore.isAdmin) {
            router.push('/admin/users');
          } else {
            router.push('/user/dishes');
          }
        }
      } finally {
        loading.value = false;
      }
    }
  });
};

const goToRegister = () => {
  router.push('/register');
};

// 社交登录处理函数
const handleWechatLogin = () => {
  ElMessage.info('微信登录功能开发中...');
};

const handleQQLogin = () => {
  ElMessage.info('QQ登录功能开发中...');
};

const handlePhoneLogin = () => {
  ElMessage.info('手机号登录功能开发中...');
};

// 组件挂载时触发动画
onMounted(() => {
  setTimeout(() => {
    cardVisible.value = true;
  }, 100);
});
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  width: 100vw;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: fixed;
  top: 0;
  left: 0;
  overflow: hidden;
}

/* 背景装饰 */
.bg-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.floating-shape {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  animation: float 6s ease-in-out infinite;
}

.shape-1 {
  width: 80px;
  height: 80px;
  top: 20%;
  left: 10%;
  animation-delay: 0s;
}

.shape-2 {
  width: 120px;
  height: 120px;
  top: 60%;
  right: 15%;
  animation-delay: 2s;
}

.shape-3 {
  width: 60px;
  height: 60px;
  bottom: 20%;
  left: 20%;
  animation-delay: 4s;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0px) rotate(0deg);
  }
  50% {
    transform: translateY(-20px) rotate(180deg);
  }
}

/* 登录卡片 */
.login-card {
  width: 380px;
  border-radius: 20px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(10px);
  background: rgba(255, 255, 255, 0.95);
  border: 1px solid rgba(255, 255, 255, 0.2);
  opacity: 0;
  transform: translateY(50px) scale(0.9);
  transition: all 0.8s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.card-entrance {
  opacity: 1;
  transform: translateY(0) scale(1);
}

.card-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 15px 0 10px;
}

.header-icon {
  font-size: 40px;
  margin-bottom: 10px;
  color: #409eff;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
}

.title {
  font-size: 24px;
  font-weight: 700;
  color: #303133;
  margin-bottom: 5px;
  background: linear-gradient(135deg, #409eff, #67c23a);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.subtitle {
  font-size: 14px;
  color: #909399;
  text-align: center;
}

/* 表单样式 */
.login-form {
  padding: 10px 25px 20px;
}

.custom-input {
  margin-bottom: 12px;
}

.custom-input :deep(.el-input__wrapper) {
  border-radius: 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  border: 2px solid transparent;
  transition: all 0.3s ease;
  padding: 10px 14px;
}

.custom-input :deep(.el-input__wrapper:hover) {
  border-color: #409eff;
  box-shadow: 0 6px 20px rgba(64, 158, 255, 0.2);
}

.custom-input :deep(.el-input__wrapper.is-focus) {
  border-color: #409eff;
  box-shadow: 0 0 0 3px rgba(64, 158, 255, 0.1);
}

.input-icon {
  color: #909399;
  font-size: 18px;
}

/* 登录按钮 */
.login-btn {
  width: 100%;
  height: 42px;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 600;
  background: linear-gradient(135deg, #409eff, #67c23a);
  border: none;
  box-shadow: 0 8px 20px rgba(64, 158, 255, 0.3);
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 12px 30px rgba(64, 158, 255, 0.4);
}

.login-btn:active {
  transform: translateY(0);
}

.login-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.5s;
}

.login-btn:hover::before {
  left: 100%;
}

/* 其他登录方式 */
.alternative-login {
  margin: 15px 0;
}

.custom-divider {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  margin-bottom: 15px;
}

.divider-line {
  flex: 1;
  height: 1px;
  background: #e4e7ed;
}

.divider-text {
  color: #909399;
  font-size: 13px;
  padding: 0 15px;
  white-space: nowrap;
  text-align: center;
}

.social-login {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 25px;
  margin-top: 0;
  width: 100%;
  padding: 0 20px;
}

.social-btn {
  width: 45px;
  height: 45px;
  border-radius: 50%;
  border: 2px solid #e4e7ed;
  transition: all 0.3s ease;
  font-size: 18px;
}

.social-btn:hover {
  transform: translateY(-3px) scale(1.1);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
}

.wechat-btn {
  color: #07c160;
  border-color: #07c160;
}

.wechat-btn:hover {
  background: #07c160;
  color: white;
}

.qq-btn {
  color: #12b7f5;
  border-color: #12b7f5;
}

.qq-btn:hover {
  background: #12b7f5;
  color: white;
}

.phone-btn {
  color: #ff6b6b;
  border-color: #ff6b6b;
}

.phone-btn:hover {
  background: #ff6b6b;
  color: white;
}

/* 底部链接 */
.footer-links {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
  margin-top: 15px;
}

.register-link {
  font-weight: 600;
  text-decoration: none;
  transition: all 0.3s ease;
}

.register-link:hover {
  transform: scale(1.05);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-card {
    width: 90%;
    margin: 20px;
  }
  
  .login-form {
    padding: 20px 30px 30px;
  }
  
  .title {
    font-size: 24px;
  }
  
  .header-icon {
    font-size: 40px;
  }
}

/* 加载动画 */
.login-btn.is-loading {
  pointer-events: none;
}

.login-btn.is-loading::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 20px;
  height: 20px;
  margin: -10px 0 0 -10px;
  border: 2px solid transparent;
  border-top: 2px solid #ffffff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
</style>

